<template>
  <el-container class="home_container">
    <!-- 头部 -->
    <el-header>
      <div>
        <i class="iconfont icon-houtaixitong"></i>
        <span>后台管理系统</span>
      </div>
      <el-button type="info" @click="logout">退出</el-button>
    </el-header>
    <el-container>
      <!-- 侧栏菜单 -->
      <el-aside :width="isCollapse?'64px':'200px'">
        <div class="toggle_button" @click="taggeleCollapse">|||</div>
        <el-menu
          background-color="#D3DCE6"
          text-color="#000"
          active-text-color="#ffd04b"
          unique-opened
          :collapse = "isCollapse" :collapse-transition="false" router :default-active="activePath">
          <el-submenu :index="item.id + ''" v-for="item in menulist" :key="item.id" >
            <!-- 一级菜单 -->
            <template slot="title">
              <!-- 图标 -->
              <i class="el-icon-location"></i>
              <!-- 文本 -->
              <span>{{item.authName}}</span>
            </template>
            <!-- 二级菜单 -->
            <el-menu-item
              :index="'/' + subitem.path "
              v-for="subitem in item.children"
              :key="subitem.id" @click="saveNavState('/' + subitem.path)"
            >
              <template slot="title">
                <i class="el-icon-location"></i>
                <span>{{subitem.authName}}</span>
              </template>
            </el-menu-item>
          </el-submenu>
        </el-menu>
      </el-aside>
      <!-- 中间菜单 -->
      <el-main>
      <router-view></router-view>
      </el-main>
    </el-container>
  </el-container>
</template>

<script>
export default {
  data () {
    return {
      menulist: [],
      isCollapse: false,
      // 被激活的链接地址
      activePath: ''
    }
  },
  created () {
    this.getMenuList()
    this.activePath = window.sessionStorage.getItem('activePath')
  },
  methods: {
    // 点击菜单 折叠与否
    taggeleCollapse () {
      this.isCollapse = !this.isCollapse
    },
    created () {
      window.sessionStorage.clear()
      this.$router.push('/login')
    },
    // 获取所有菜单
    async getMenuList () {
      const { data: res } = await this.$http.get('menus')
      if (res.meta.status !== 200) return this.$message.error(res.meta.msg)
      this.menulist = res.data
      // console.log(res)
    },
    logout () {
      window.sessionStorage.clear()
      this.$router.push('/login')
    },
    // 保存连接激活状态
    saveNavState (activePath) {
      window.sessionStorage.setItem('activePath', activePath)
      this.activePath = activePath
    }
  }
}
</script>

<style lang="less" scoped>
.home_container{
    height: 100%;
}
.el-header{
    background-color: #B3C0D1;
   display: flex;
   justify-content: space-between;
   align-items: center;
  }
.icon-houtaixitong{
    margin: 0 10px;
}
  .el-aside {
    background-color: #D3DCE6;
    overflow: hidden;
    .el-menu{
      border-right: none;
    }
    .toggle_button{
      background-color: gainsboro;
      color: #fff;
      font-size: 14px;
      line-height: 20px;
      text-align: center;
      letter-spacing: 0.2em;
      cursor: pointer;
    }
  }
  .el-main {
    background-color: #E9EEF3;
  }
</style>
